// pages/shop/good/goods.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    indicatorDots: true,
    autoplay: true,
    interval: 2000,
    circular: true,
    duration: 500,
    navbarActiveIndex: 0,
    navbarTitle: [
      "商品介绍",
      "规格参数",
    ],
    goodsInfo:[],
    banners:[]
  },
  /**
   * 点击导航栏
   */
  onNavBarTap: function (event) {
    // 获取点击的navbar的index
    let navbarTapIndex = event.currentTarget.dataset.navbarIndex
    // 设置data属性中的navbarActiveIndex为当前点击的navbar
    this.setData({
      navbarActiveIndex: navbarTapIndex
    })
  },

  /**
   *
   */
  onBindAnimationFinish: function ({
    detail
  }) {
    // 设置data属性中的navbarActiveIndex为当前点击的navbar
    this.setData({
      navbarActiveIndex: detail.current
    })
  },
  queryGoodsInfo:function(id){
    let _this=this;
    wx.request({
      url: 'https://dev.beiyouedu.com/cf/goods/get',
      method: 'GET',
      data: {
        id: id,
      },
      success: function (res) {
        let result = res.data.data;
        let newResult=result.good_desc.replace(/\<img/gi, '<img style="width:100%;height:auto" ');
        result.good_desc=newResult;
        let banner=result.banners;
        if (res.data.code == 200) {
          _this.setData({
            goodsInfo: result,
            banners:banner
          });
        }
      }
    });
  },
  addToCar:function(){
    let flag=true;
    let goods={
      info:this.data.goodsInfo,
      num:1
    }
    let goodsCar=wx.getStorageSync('shopItems');
    if(goodsCar.length==0){
      goodsCar=new Array();
    }
    for(let i=0;i<goodsCar.length;i++){
      if(goods.info.id==goodsCar[i].info.id){
        goodsCar[i].num+=goods.num;
        flag=false;
      }
    }
    if(flag){
      goodsCar.push(goods);
    }
    wx.setStorageSync('shopItems', goodsCar);
    wx.showToast({
      title: '加入购物车成功...',
      icon: 'none',
      mask: true
    });
  },
  toGoCar:function(){
    wx.navigateTo({
      url: '/pages/shop/goodsCar/goodsCar',
    })
    },
    //下单
    buyGoods:function(){
      let goods={
        banners:this.data.goodsInfo.banners[0],
        good_name:this.data.goodsInfo.good_name,
        cf_price:this.data.goodsInfo.cf_price,
        num:1
      };
      let temp=[];
      temp.push(goods);
      wx.navigateTo({
        url: '/pages/order/detail?good='+JSON.stringify(temp),
      })
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.queryGoodsInfo(options.id);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})